package com.ctrip.cap.appium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.browserlaunchers.Sleeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.selenium.Wait;

/**
 * Wait是自动化中最重要的对象。方法元素带Page的是为PageObject准备的.
 * 
 * @see ExpectedConditions,Sleeper
 * 
 * @author ltyao
 *
 */
public class WaitUtils {

	private static final Logger logger = LoggerFactory
			.getLogger(WaitUtils.class);

	public static boolean isPageElementExist(final WebElement element) {
		try {
			untilPageElementExist(element);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static boolean isPageElementExist(final WebElement element,
			long timeoutInSeconds) {
		try {
			untilPageElementExist(element, timeoutInSeconds);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static void untilPageSourceContains(final WebDriver driver,
			final String content, long timeoutInSeconds) {
		new Wait() {
			@Override
			public boolean until() {
				try {
					if (driver.getPageSource().contains(content))
						return true;
				} catch (Exception e) {
					logger.debug("", e);
				}
				return false;
			}
		}.wait(String.format("Wait PageSource contains (%s)", content),
				timeoutInSeconds * 1000);
	}

	/**
	 * for Page Object
	 * 
	 * @param element
	 */
	public static void untilPageElementExist(final WebElement element) {
		untilPageElementExist(element, 30);
	}

	/**
	 * 
	 * @param element
	 * @param timeoutInSeconds
	 */
	public static void untilPageElementExist(final WebElement element,
			long timeoutInSeconds) {
		new Wait() {
			@Override
			public boolean until() {
				try {
					element.getLocation();
					return true;
				} catch (Exception e) {
					logger.debug("", e);
				}
				return false;
			}
		}.wait(String.format("Wait Page Element(%s) to be exist", element),
				timeoutInSeconds * 1000);
	}

	/**
	 * 
	 * @param element
	 */
	public static void untilPageElementVisible(final WebElement element) {
		untilPageElementVisible(element, 30);

	}

	/**
	 * 
	 * @param element
	 * @param timeoutInSeconds
	 */
	public static void untilPageElementVisible(final WebElement element,
			long timeoutInSeconds) {

		new Wait() {
			@Override
			public boolean until() {
				try {
					return element.isDisplayed();
				} catch (Exception e) {
					// logger.warn("", e);
				}
				return false;
			}
		}.wait(String.format("Wait Page Element(%s) to be Visible", element),
				timeoutInSeconds * 1000);
	}

	public static void sleepTightInSeconds(long timeoutInSeconds) {
		Sleeper.sleepTightInSeconds(timeoutInSeconds);
	}

	/**
	 * Sleeps without explicitly throwing an InterruptedException
	 *
	 * @param timeout
	 *            the amout of time to sleep
	 * @throws RuntimeException
	 *             wrapping an InterruptedException if one gets thrown
	 */
	public static void sleepTight(long timeoutInMills) {
		Sleeper.sleepTight(timeoutInMills);
	}
}
